#include "Ball.h"

namespace coolpool
{
	Ball::Ball(const Ball & ball) :
		RigidBody(ball)
	{
		bounding_cylinder_ = ball.bounding_cylinder_;
		radius_ = ball.radius_;
		texture_ = ball.texture_;
	}

	Ball::Ball(double radius) :
		RigidBody()
	{
		if (radius <= 0.0)
			throw NoSuchBallException();

		bounding_cylinder_.setRadius(radius);

		radius_ = radius;
		init();
	}

	void Ball::init()
	{
		math_tools::Matrix3D m;
		matrix_tools::identityMatrix(m);
		m[0] = (2.0 / 5.0) * getMass() * (radius_)* (radius_ );
		m[5] = m[0];
		m[10] = m[0];
		m[15] = 1;
		setInertiaTensor(m);
		m[0] = 1.0 / m[0];
		m[5] = m[0];
		m[10] = m[0];
		setInertiaTensorInverse(m);
	}

	Ball::~Ball()
	{

	}

	Entity * Ball::clone() const
	{
		return new Ball(*this);
	}

	int Ball::getType() const
	{
		return CP_BALL;
	}

	const BoundingCylinder & Ball::getBoundingCylinder()
	{
		Eigen::Vector2d center;
		center[0] = getCenterOfMassLocation().getX();
		center[1] = getCenterOfMassLocation().getZ();
		bounding_cylinder_.setCenter(center);
		return bounding_cylinder_;
	}

	IRenderable * Ball::getRenderable()
	{
		return new BallRenderable(getCenterOfMassLocation(), getCurrentOrientation(), radius_, texture_);
	}
}
